import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie, Bar, Grid
# 读取文件
data = pd.read_csv('final_jobinfo.csv')

companys = []
values = []
counts = []
# 统计公司类型数量，按照公司类型进行分组统计
data = data['公司类型'].groupby(data['公司类型']).value_counts()
for key, value in zip(data.keys(), data.values):
    # print(key[0])
    # print(value)
    companys.append(key[0].strip())
    counts.append(int(value))
    values.append("%.1f" % ((int(value) / data.values.sum()) * 100))

pie = (
    Pie().add("公司类型统计",[list(z) for z in zip(companys, values)],
        center=["25%", "50%"],)
        .set_global_opts(
        title_opts=opts.TitleOpts(title="公司类型统计",pos_left="25%"),
        legend_opts=opts.LegendOpts(is_show=False),)
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}%"))
)

bar = (
    Bar()
        .add_xaxis(companys)
        .add_yaxis("数量", counts)
        .set_global_opts(legend_opts=opts.LegendOpts(pos_right="20%"))
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
)

# 将饼图和柱状图合并成一个图形
grid = (
    Grid(init_opts=opts.InitOpts(width="1800px", height="720px"))
        .add(bar, grid_opts=opts.GridOpts(pos_left="68%"), is_control_axis_index=True)
        .add(pie, grid_opts=opts.GridOpts(pos_right="40%"), is_control_axis_index=True)
        .render("公司类型和数量占比统计.html")
)